<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>lazy-nav</title>

    <style>
        *{
            margin: 0;
            padding: 0;
        }
        .container {
            width: 960px;
            height: 600px;
            border: 1px solid #e74f4d;
            margin: 20px auto;
        }
        #nav {
            list-style: none;
            width: 100%;
            height: 56px;
            background-color: orange;
        }
        #nav li{
            float: left;
            text-align: center;
            line-height: 56px;
            width: 120px;

        }
        #nav .sub_item {
            line-height: 30px;
        }
        #nav a{
            display: block;
            text-decoration: none;
            font-size: 18px;
            color: #ffffff;
        }
        #nav .sub_item a{
            color: gray;
        }
        #nav .sub_item a:hover{
            background-color: orange;
        }
        #nav li a:first-child:hover{
            background-color: darkorange;
        }
        .sub_item {
            width: 110px;
            height: 100%;
            border: 1px solid lightgray;
            margin-left: 10px;
            position: relative;
            display: none;
        }
        .sub_item .arrow{
            display: block;
            width:0;
            height: 0;
            border-top: 11px solid transparent;
            border-right: 8px solid transparent;
            border-left: 8px solid transparent;
            border-bottom: 11px solid #fff;
            position: absolute;
            top: -22px;
            left: 40%;
        }
    </style>
</head>
<body>
    <div class="container">
        <ul id="nav">
            <li class="item">
                <a href="#" class="normal">首页</a>
                <div class="sub_item">
                    <span class="arrow"></span>
                    <a href="#">首页-A</a>
                    <a href="#">首页-B</a>
                    <a href="#">首页-C</a>
                </div>
            </li>
            <li class="item">
                <a href="#" class="normal">关于</a>
                <div class="sub_item">
                    <span class="arrow"></span>
                    <a href="#">关于-A</a>
                    <a href="#">关于-B</a>
                    <a href="#">关于-C</a>
                </div>
            </li>
            <li class="item">
                <a href="#" class="normal">联系</a>
                <div class="sub_item">
                    <span class="arrow"></span>
                    <a href="#">联系-A</a>
                    <a href="#">联系-B</a>
                    <a href="#">联系-C</a>
                </div>
            </li>
        </ul>
    </div>

    <script>
        window.onload = function () {
            //元素节点
            var a_list = document.getElementsByClassName('normal');
            var subItem_list = document.getElementsByClassName('sub_item');
            var timer = null;

            for(var i = 0; i<a_list.length; i++){
                a_list[i].index = i;

                //mouseover显示子菜单
                a_list[i].onmouseover = function () {
                    for(var j = 0; j<subItem_list.length; j++){
                        subItem_list[j].style.display = "none";
                    }
                    subItem_list[this.index].style.display = "block";

                    //移出子菜单后，a又获得焦点
                    clearTimeout(timer);
                };

                //mouseout隐藏子菜单
                a_list[i].onmouseout = function () {
                    var self = this;
                    timer = setTimeout(function () {
                        subItem_list[self.index].style.display = "none";
                    }, 500)
                };

                //子菜单隐藏前获得焦点
                subItem_list[i].onmouseover = function () {
                    clearTimeout(timer);
                };

                //子菜单失去焦点，隐藏
                subItem_list[i].onmouseout = function () {
                    var self = this;
                    timer = setTimeout(function () {
                        self.style.display = "none";
                    }, 500)
                };
            }
        }
    </script>
</body>
</html>